<template>
	<el-container>
		<el-aside style="width: 33%">
			<el-container>
				<el-header>
					<div class="left-panel">
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="search.keyword" placeholder="项目名称" clearable></el-input>
							<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="apiObj" row-key="projectId" hidePagination @row-click="showProject">
						<el-table-column label="项目名称" prop="title" width="150" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="项目编号" prop="projectCode" width="100" :show-overflow-tooltip="true"></el-table-column>
						<el-table-column label="负责人" prop="chargeUserName" width="80"></el-table-column>
						<el-table-column label="项目预算" prop="totalCost" width="80">
							<template #default="scope">
								{{scope.row.totalCost}}/万元
							</template>
						</el-table-column>
						<el-table-column label="操作" fixed="right" align="right" width="120">
							<template #default="scope">
								<el-button-group>
									<el-button text type="primary" size="small" @click.stop="addChildProject(scope.row, scope.$index)">添加</el-button>
									<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="scope.row.parentId!='0'">
										<template #reference>
											<el-button text type="primary" size="small">删除</el-button>
										</template>
									</el-popconfirm>
								</el-button-group>
							</template>
						</el-table-column>
					</scTable>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="text-align: center;display: block;">
				<div style="font-weight: 600;font-size: 20px;">项目详情</div>
			</el-header>
			<el-main class="nopadding">
				<el-card>
					<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
						<el-form-item label="项目名称" prop="title">
							<el-input v-model="form.title" placeholder="项目名称" clearable maxlength="80"></el-input>
						</el-form-item>
						<el-row :gutter="24">
							<el-col :lg="12">
								<el-form-item label="项目类型" prop="projectType">
									<sc-select v-model="form.projectType" :params="{code:'budgetProType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
								</el-form-item>
							</el-col>
							<el-col :lg="12">
								<el-form-item label="项目编号" prop="projectCode">
									<el-input v-model="form.projectCode" placeholder="项目编号" clearable maxlength="80"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="24">
							<el-col :lg="12">
								<el-form-item label="项目负责人" prop="chargeUser">
									<select-user-dialog v-model:accountId="form.chargeUser" :mode="false"/>
								</el-form-item>
							</el-col>
							<el-col :lg="12">
								<el-form-item label="预算总费用" prop="totalCost">
									<el-input-number v-model="form.totalCost" placeholder="预算总费用" clearable></el-input-number>
									/万元
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="24">
							<el-col :lg="24">
								<el-form-item label="项目参与人" prop="joinUser">
									<select-user-dialog v-model:accountId="form.joinUser" :mode="true"/>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="24">
							<el-col :lg="12">
								<el-form-item label="开始时间" prop="beginTime">
									<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
								</el-form-item>
							</el-col>
							<el-col :lg="12">
								<el-form-item label="结束时间" prop="endTime">
									<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row :gutter="24">
							<el-col :lg="12">
								<el-form-item label="所属账套" prop="budgetAccount">
									<el-cascader v-model="form.budgetAccount" :options="budgetAccountList" :props="budgetAccountProps" clearable :show-all-levels="false" style="width: 100%;"></el-cascader>
								</el-form-item>
							</el-col>
							<el-col :lg="12">
								<el-form-item label="资金出处" prop="cashFrom">
									<el-input v-model="form.cashFrom" placeholder="资金出处" clearable maxlength="80"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<el-form-item label="项目说明">
							<sc-editor v-model="form.remark" placeholder="请输入" :height="400" clearable style="width: 100%;"></sc-editor>
						</el-form-item>
						<el-form-item label="项目附件" prop="attachId">
							<sc-upload-file :apiObj="apiFileObj" v-model="fileList" :data="{module:'budget'}" :showFileList="false" @success="addNewFile">
								<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
							</sc-upload-file>
							<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
						</el-form-item>
					</el-form>
				</el-card>
			</el-main>
			<el-footer>
				<div style="text-align: center;">
					<el-button v-if="mode=='edit'" type="primary" :loading="isSaveing" @click="submit()">更 新</el-button>
					<el-button v-if="mode=='add'" type="primary" :loading="isSaveing" @click="addProject()">保 存</el-button>
				</div>
			</el-footer>
		</el-container>
	</el-container>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import selectUserDialog from "@/views/module/selectUserDialog";
import {defineAsyncComponent} from "vue";

const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	name: 'Budget Adjustment Manage',
	components: {
		AttachMenu,
		selectUserDialog,
		scEditor
	},
	data() {
		return {
			apiObj: this.$API.finance.budget.getAllBudgetProjectList,
			apiFileObj: this.$API.file.attach.uploadFile,
			selection: [],
			isSaveing: false,
			fileList: [],
			mode: "add",
			search: {
				keyword: '',
			},
			budgetAccountList: [],
			budgetAccountProps: {
				label: "title",
				value: "budgetAccountId",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				projectId: '',
				title: '',
				projectType: '1',
				projectCode: '',
				chargeUser: '',
				totalCost: 0,
				joinUser: '',
				beginTime: '',
				endTime: '',
				budgetAccount: '',
				cashFrom: '',
				remark: '',
				attachId: ''
			},
			rules: {
				title: [
					{required: true, message: '项目名称不能为空！', trigger: 'blur'}
				],
				projectType: [
					{required: true, message: '项目类型不能为空！', trigger: 'blur'}
				],
				projectCode: [
					{required: true, message: '项目编号不能为空！', trigger: 'blur'}
				],
				beginTime:[
					{required: true, message: '开始时间不能为空！', trigger: 'blur'}
				],
				endTime:[
					{required: true, message: '结束时间不能为空！', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {
		this.getBudgetProjectList();
	},
	methods: {
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		showProject(row, index) {
			this.mode = "edit"
			this.form.projectId = row.projectId
			this.form.title = row.title
			this.form.projectType = row.projectType
			this.form.projectCode = row.projectCode
			this.form.chargeUser = row.chargeUser
			this.form.totalCost = row.totalCost
			this.form.joinUser = row.joinUser
			this.form.beginTime = row.beginTime
			this.form.endTime = row.endTime
			this.form.budgetAccount = row.budgetAccount
			this.form.cashFrom = row.cashFrom
			this.form.remark = row.remark
			this.form.attachId = row.attachId
		},
		async getUserListByAccountIds(accountIds) {
			let res = await this.$API.system.user.getUserListByAccountIds.get({accountIds: accountIds});
			if (res.code == 200) {
				let arr = [];
				if (res.data != undefined) {
					res.data.forEach(item => {
						arr.push(item.userName);
					})
				}
				this.form.joinUserName = arr.join(",");
			}
		},
		async getBudgetProjectList() {
			var res = await this.$API.finance.budget.getAllBudgetAccountList.get();
			this.budgetAccountList = res.data;
		},
		async getAttachList(str) {
			var res = await this.$API.file.attach.getAttachList.get({attachIds: str});
			if (res.code == 200) {
				this.fileList = res.data;
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		addChildProject(row, index) {
			this.mode = "add";
			this.form = {}
			this.form.parentId = row.projectId
		},
		//删除
		async table_del(row) {
			var reqData = {projectId: row.projectId}
			var res = await this.$API.finance.budget.deleteBudgetProject.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.finance.budget.updateBudgetProject.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$refs.table.upData(this.search)
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addProject(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.finance.budget.insertChildBudgetProject.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$refs.table.upData(this.search)
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
